package com.wit.health.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wit.health.R;
import com.wit.health.pojo.Action;

import java.util.List;

public class ExerciseAdapter extends RecyclerView.Adapter<ExerciseAdapter.ExerciseViewHolder> {

    private List<Action> exerciseList;
    private OnItemClickListener onItemClickListener;

    public ExerciseAdapter(List<Action> exerciseList) {
        this.exerciseList = exerciseList;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ExerciseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.exercise_item, parent, false);
        return new ExerciseViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ExerciseViewHolder holder, int position) {
        Action exercise = exerciseList.get(position);
        holder.exerciseImageView.setImageResource(exercise.getImageResourceId());
        holder.exerciseNameTextView.setText(exercise.getName());


        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(exercise);
            }
        });
    }

    @Override
    public int getItemCount() {
        return exerciseList.size();
    }

    public static class ExerciseViewHolder extends RecyclerView.ViewHolder {
        public ImageView exerciseImageView;
        public TextView exerciseNameTextView;

        public ExerciseViewHolder(View itemView) {
            super(itemView);
            exerciseImageView = itemView.findViewById(R.id.exercise_image);
            exerciseNameTextView = itemView.findViewById(R.id.exercise_name);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Action exercise);

    }

}
